package com.lindroy.jetpacknotes.lifecycle.lifecycle_ui.new_way

import android.util.Log
import androidx.lifecycle.*

/**
 * @author Lin
 * @date 2021/12/20
 * @function
 */
private const val TAG = "Locate"

/**
 * 1、继承 LifecycleObserver，使用注解的方式，已废弃
 */
class LocationListener : LifecycleObserver {
    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun startLocate() {
        Log.d(TAG, "Start Locate!")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun stopLocate() {
        Log.d(TAG, "Stop Locate!")
    }
}

/**
 * 2、继续 LifecycleEventObserver 接口，并复写 onStateChanged 方法
 */
class LocationListener2 : LifecycleEventObserver {

    override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
        when (event) {
            Lifecycle.Event.ON_START -> Log.d(TAG, "Start Locate!")
            Lifecycle.Event.ON_STOP -> Log.d(TAG, "Stop Locate!")
            else -> {}
        }
    }
}

/**
 * 3、继续 DefaultLifecycleObserver 接口，并复写各个生命周期的方法
 */
class LocationListener3 : DefaultLifecycleObserver {

    override fun onStart(owner: LifecycleOwner) {
        super.onStart(owner)
        Log.d(TAG, "Start Locate!")
    }

    override fun onStop(owner: LifecycleOwner) {
        super.onStop(owner)
        Log.d(TAG, "Stop Locate!")
    }
}
